package com.sr.controller;

import com.sr.constant.Constant;
import com.sr.enums.ResponseCode;
import com.sr.pojo.SysUser;
import com.sr.util.JwtTokenUtil;
import com.sr.util.R;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xiaochi
 * @date 2022/4/29 17:36
 * @desc BaseController
 */
public abstract class BaseController {

    private ServletRequestAttributes getServletRequestAttributes(){
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
    }

    protected HttpServletRequest getRequest(){
        return this.getServletRequestAttributes().getRequest();
    }

    protected HttpServletResponse getResponse(){
        return this.getServletRequestAttributes().getResponse();
    }

    /**
     * 从 request 中获取 token
     * @return
     */
    protected String getToken(){
        String token = getRequest().getHeader(Constant.User.ACCESS_TOKEN);
        if (token != null && !"".equals(token)){
            return token;
        }
        return token;
    }

    /**
     * 从 request 中获取 token
     * @param request
     * @return
     */
    protected String getToken(HttpServletRequest request){
        String token = request.getHeader(Constant.User.ACCESS_TOKEN);
        if (token != null && !"".equals(token)){
            return token;
        }
        return token;
    }

    /**
     * 从 request 中获取 userId
     * @return
     */
    protected String getUserId(){
        return this.getToken(this.getRequest());
    }

    /**
     * 从 request 中获取 userId
     * @param request
     * @return
     */
    protected String getUserId(HttpServletRequest request){
        return this.getToken(request);
    }

    /**
     * 从 accessToken 中获取 userId
     * @param token
     * @return
     */
    protected String getUserId(String token){
        return JwtTokenUtil.getSubject(token);
    }

    /**
     * 获取登录账户username
     * @return
     */
    protected String getUsername(){
        return getLoginUser().getUsername();
    }

    /**
     * 获取当前登录用户，也可以获取token中的用户
     * @return
     */
    protected SysUser getLoginUser(){
        // todo:
        //return (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return null;
    }

    /**
     * 返回 R<String>
     * @param result
     * @return
     */
    protected R<String> response(int result){
        if (result > 0){
            return R.ok(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getMessage());
        }
        return R.error(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getMessage());
    }

    /**
     * 返回 R<String>
     * @param expression
     * @return
     */
    protected R<String> response(Type expression){
        return this.response(expression,"");
    }

    /**
     * 返回 R<String>
     * @param expression
     * @param message
     * @return
     */
    @SuppressWarnings("all")
    protected R<String> response(Type expression,String message){
        Object t = expression.get();
        if (t instanceof Integer){
            if ((Integer) t > 0){
                return R.ok();
            }
            if (StringUtils.isEmpty(message)){
                return R.error();
            }
            return R.error(message);
        }
        return R.error();
    }

    /**
     * 返回 R<List<T>>
     * @param expression
     * @param clazz
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    protected <T>R<List<T>> response(Type expression,Class<T> clazz){
        Object t = expression.get();
        List<T> val = new ArrayList<>();
        if (t instanceof List){
            val = (List<T>)t;
            return R.ok(val);
        }
        return R.ok(val);
    }

    @FunctionalInterface
    interface Type{
       Object get();
    }

    /**
     * 获取值类型
     * @param val
     * @return
     */
    protected String getType(Object val){
        if (val instanceof Integer){
            return "Integer";
        }
        if (val instanceof String){
            return "String";
        }
        if (val instanceof Double){
            return "Double";
        }
        if (val instanceof Float){
            return "Float";
        }
        if (val instanceof Long){
            return "Long";
        }
        if (val instanceof Boolean){
            return "Boolean";
        }
        if (val instanceof Date){
            return "Date";
        }
        if (val instanceof List){
            return "List";
        }
        return null;
    }
}